package com.mtoolkit.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A utility class that provides record logger conveniently.
 * 
 * It depends on <code>apache commons-logging</code> component packet,
 * you can get it from <a href="">http://commons.apache.org/logging/</a>.
 * 
 * @author 	<a href="mailto:xishizhang@gmail.com">ZhangShixi</a>
 * @version 1.0, 29/9/2011
 * @since 	JDK1.5
 */
public class LogUtil {

	/** record logger instance */
    private Log _log;

    /**
     * Creates an {@code LogUtil} instance with the specified {@link Log} instance.
     * 
     * @param  log common log instance.
     * 
     * @throws NullPointerException if {@code log} is null.
     */
    public LogUtil(Log log) {
    	if (log == null) {
    		throw new NullPointerException("log");
    	}
    	
        this._log = log;
    }
    
    /**
     * Creates an {@code LogUtil} instance with the specified class name.
     * 
     * @param  name logger class name.
     * 
     * @return logger utility instance.
     */
    public static LogUtil getLogger(String name) {
    	return new LogUtil(LogFactory.getLog(name));
    }
    
    /**
     * Creates an {@code LogUtil} instance with the specified class.
     * 
     * @param  clazz logger class. 
     * 
     * @return logger utility instance.
     */
    public static LogUtil getLogger(Class<?> clazz) {
        return new LogUtil(LogFactory.getLog(clazz));
    }
    
    // ---- info log -------------------------------------------------------
    /**
     * Records the specified info log message.
     * 
     * @param message log message.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void info(String message) {
        info(message, (Object[]) null);
    }

    /**
     * Records the specified info log message with the specified parameters.
     * 
     * @param message log message.
     * @param params  message parameters.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void info(String message, Object... params) {
        if (message != null) {
            if (_log.isInfoEnabled()) {
                _log.info(StringUtil.replaceHolderArgs(message, params));
            }
        }
    }
    
    /**
     * Records the specified info log message for an error.
     * 
     * @param message   log message.
     * @param throwable exception.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void info(String message, Throwable throwable) {
        if (message != null) {
            if (_log.isInfoEnabled()) {
                _log.info(message, throwable);
            }
        }
    }
    
    // ---- debug log ------------------------------------------------------
    /**
     * Records the specified debug log message.
     * 
     * @param message log message.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void debug(String message) {
        debug(message, (Object[]) null);
    }

    /**
     * Records the specified debug log message with the specified parameters.
     * 
     * @param message log message.
     * @param params  message parameters.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void debug(String message, Object... params) {
        if (message != null) {
            if (_log.isDebugEnabled()) {
                _log.debug(StringUtil.replaceHolderArgs(message, params));
            }
        }
    }

    /**
     * Records the specified debug log message for an error.
     * 
     * @param message   log message.
     * @param throwable exception.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void debug(String message, Throwable throwable) {
        if (message != null) {
            if (_log.isDebugEnabled()) {
                _log.debug(message, throwable);
            }
        }
    }

    // ---- warn log -------------------------------------------------------
    /**
     * Records the specified warn log message.
     * 
     * @param message log message.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void warn(String message) {
        warn(message, (Object[]) null);
    }

    /**
     * Records the specified warn log message with the specified parameters.
     * 
     * @param message log message.
     * @param params  message parameters.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void warn(String message, Object... params) {
        if (message != null) {
            if (_log.isWarnEnabled()) {
                _log.warn(StringUtil.replaceHolderArgs(message, params));
            }
        }
    }

    /**
     * Records the specified warn log message for an error.
     * 
     * @param message   log message.
     * @param throwable exception.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void warn(String message, Throwable throwable) {
        if (message != null) {
            if (_log.isWarnEnabled()) {
                _log.warn(message, throwable);
            }
        }
    }

    // ---- error log ------------------------------------------------------
    /**
     * Records the specified error log message.
     * 
     * @param message log message.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void error(String message) {
        error(message, (Object[]) null);
    }

    /**
     * Records the specified error log message with the specified parameters.
     * 
     * @param message log message.
     * @param params  message parameters.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void error(String message, Object... params) {
        if (message != null) {
            if (_log.isErrorEnabled()) {
                _log.error(StringUtil.replaceHolderArgs(message, params));
            }
        }
    }

    /**
     * Records the specified error log.
     * 
     * @param throwable exception.
     */
    public void error(Throwable throwable) {
        if (throwable != null) {
            error(throwable.getMessage(), throwable);
        }
    }
    
    /**
     * Records the specified error log message for an error.
     * 
     * @param message   log message.
     * @param throwable exception.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void error(String message, Throwable throwable) {
        if (message != null) {
            if (_log.isErrorEnabled()) {
                _log.error(message, throwable);
            }
        }
    }

    // ---- fatal log ------------------------------------------------------
    /**
     * Records the specified fatal log message.
     * 
     * @param message log message.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void fatal(String message) {
        fatal(message, (Object[]) null);
    }

    /**
     * Records the specified fatal log message with the specified parameters.
     * 
     * @param message log message.
     * @param params  message parameters.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void fatal(String message, Object... params) {
        if (message != null) {
            if (_log.isFatalEnabled()) {
                _log.fatal(StringUtil.replaceHolderArgs(message, params));
            }
        }
    }
    
    /**
     * Records the specified fatal log message for an error.
     * 
     * @param message   log message.
     * @param throwable exception.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void fatal(String message, Throwable throwable) {
        if (message != null) {
            if (_log.isFatalEnabled()) {
                _log.fatal(message, throwable);
            }
        }
    }

    // ---- trace log ------------------------------------------------------
    /**
     * Records the specified trace log message.
     * 
     * @param message log message.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void trace(String message) {
        trace(message, (Object[]) null);
    }

    /**
     * Records the specified trace log message with the specified parameters.
     * 
     * @param message log message.
     * @param params  message parameters.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void trace(String message, Object... params) {
        if (message != null) {
            if (_log.isTraceEnabled()) {
                _log.trace(StringUtil.replaceHolderArgs(message, params));
            }
        }
    }

    /**
     * Records the specified trace log message for an error.
     * 
     * @param message   log message.
     * @param throwable exception.
     * 
     * @throws NullPointerException if {@code message} is null.
     */
    public void trace(String message, Throwable throwable) {
        if (message != null) {
            if (_log.isTraceEnabled()) {
                _log.trace(message, throwable);
            }
        }
    }

}
